<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>备案</title>
    <style type="text/css">
        td{
            padding: 2px 0px;
            text-align: left;
            border: 1px solid #999;
        }
        input{width: 239px;}
    </style>
    <link href="/resources/css/table.css?r=3" rel="stylesheet">
    <link href="/resources/css/fk.css?r=3" rel="stylesheet">
    <link href="/resources/css/fk_2.css?r=3" rel="stylesheet">
    <script src="/resources/js/jquery-1.11.1.min.js"></script>
    <script src="/resources/js/self.js"></script>
    <script type="text/javascript" src="/resources/js/md5.js"></script>
    <script type="text/javascript" src="/resources/My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" src="/resources/js/ajaxfileupload.js"></script>
    <script type="text/javascript" src="/resources/js/Util.js?v=2"></script>
    <script type="text/javascript" src="/resources/js/idCardUtil.js"></script>
    <link  href="/resources/viewer/viewer.css" rel="stylesheet">
    <script src="/resources/viewer/viewer.js"></script>
</head>
</head>
<body style="background-color: #fff !important;">


<div class="table-container" >
    <div style="font-size: 30px;width:80%;margin: auto">
        <div style="float: left">
            <img src="/resources/images/jdbz.png" style="height: 40px;width: 40px"/>
        </div>
        <div style="float: left">
            <span style="padding-left: 5px">禁毒清源系统</span>
        </div>
        <div style="clear:both"></div>

    </div>
    <div class="zctitle">
        企业备案信息
    </div>
    <table style="width:1200px">

        <tr>
            <td class="name" rowspan="5" style="font-size:20px;width: 12%">
                备案单位
                <input type="hidden" id="id" name="id"
                       class="form-control form-data ">
            </td>
            <td class="name">名称</td>
            <td>
                <input type="text" id="name" name="name"
                       class="form-control form-data ">
            </td>
            <td class="name">
                地址
            </td>
            <td>
                <input type="text" id="address" name="address"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name">
                法人代表
            </td>
            <td>
                <input type="text"  name="legalPerson"
                       class="form-control form-data ">
            </td>
            <td class="name">
                身份证号
            </td>
            <td>
                <input type="text"  name="idCard"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name">
                固定电话
            </td>
            <td>
                <input type="text"  name="telephone"
                       class="form-control form-data ">
            </td>
            <td class="name">
                移动电话
            </td>
            <td>
                <input type="text"  name="mobile"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name">邮箱</td>
            <td>
                <input type="text"  name="email"
                       class="form-control form-data ">
            </td>
            <td class="name">传真</td>
            <td>
                <input type="text"  name="fax"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name">
                网址
            </td>
            <td colspan="3">
                <input type="text" style="width: 894px"  name="webSite"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name" colspan="2">
                危化品从业类型
            </td>
            <td>
                <input type="text"  name="practiceType"
                       class="form-control form-data ">
            </td>
            <td class="name">
                安监部门颁发证书号
            </td>
            <td>
                <input type="text"  name="securityNum"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name" colspan="2">
                危化品从业批准机关(市、州、地方安监部门)
            </td>
            <td>
                <input type="text"  name="securityDept"
                       class="form-control form-data ">
            </td>
            <td class="name">
                安监部门颁发证件有效期
            </td>
            <td>
                <input type="text"  name="securityTime"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr style="background-color:#444">
            <td class="name" rowspan="2">
                备案登记申办人
            </td>
            <td class="name">姓名</td>
            <td>
                <input type="text"  name="applyPeople"
                       class="form-control form-data ">
            </td>
            <td class="name">身份证号</td>
            <td>
                <input type="text"  name="aIdCard"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr style="background-color:#444">
            <td class="name">固定电话</td>
            <td>
                <input type="text"  name="aTelephone"
                       class="form-control form-data ">
            </td>
            <td class="name">移动电话</td>
            <td>
                <input type="text"  name="aMobile"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name" colspan="2">保管人员</td>
            <td>
                <input type="text"  name="keepPeople"
                       class="form-control form-data ">
            </td>
            <td class="name">联系人及手机</td>
            <td>
                <input type="text"  name="kMobile"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name" colspan="2">安全保卫负责人</td>
            <td>
                <input type="text"  name="securityPeople"
                       class="form-control form-data ">
            </td>
            <td class="name">危化品从业(人数)</td>
            <td>
                <input type="text"  name="peopleNum"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name" colspan="2">环保资质有效期</td>
            <td>
                <input type="text"  name="environmentTime"
                       class="form-control form-data ">
            </td>
            <td class="name">运输车辆（合计）</td>
            <td>
                <input type="text"  name="truckNum"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name" colspan="2">工商营业执照有效期</td>
            <td>
                <input type="text"  name="licenseTime"
                       class="form-control form-data ">
            </td>
            <td class="name">危化品存储地</td>
            <td>
                <input type="text"  name="area"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name" colspan="2">危化品是否涉及出入境</td>
            <td>
                <input type="text"  name="transnational"
                       class="form-control form-data ">
            </td>
            <td class="name">库房有无监控、监控设备服务器品牌</td>
            <td>
                <input type="text"  name="monitorInfo"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr class="product-title">
            <td class="name" colspan="2">涉及品种</td>
            <td class="name" colspan="2">年消耗量</td>
            <td class="name" colspan="2"><button  class="btn btn-success" onclick="addProduct()">添加</button></td>
        </tr>
        <tr class="product-content">
            <td colspan="2">
                <input type="text"  class="product"    style="width: 245px;"
                       class="form-control form-data ">
            </td>
            <td colspan="2">
                <input type="text" class="waste"  style="width: 615px;"
                       class="form-control form-data ">
            </td>
            <td class="name" colspan="2">
                <button class="product-remove delete">删除</button>
            </td>
        </tr>

        <tr class="product-content-after">
            <td class="name" colspan="2">
                生产、经营、存储、使用、销毁危化品的品名、数量
            </td>
            <td colspan="3">
                <input type="text"  name="pInfo" style="width:894px"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name" colspan="2">
                存储设施及安全保卫措施
            </td>
            <td colspan="3">
                <input type="text"  name="keepInfo" style="width:894px"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name" colspan="2">
                危化品用途
            </td>
            <td colspan="3">
                <input type="text"  name="pUseInfo" style="width:894px"
                       class="form-control form-data ">
            </td>
        </tr>
        <tr>
            <td class="name" colspan="5">
                所需提供资料原件复印件
                <input type="file" id="file_input" name="file" onchange="uploadFile('file')" value="Upload your resume"
                       style="opacity: 0;width: 0px;display: none">
                <button  class="btn btn-success" onclick="$('#file_input').click();" >
                    上传文件
                </button>
            </td>
        </tr>
        <tr>
            <td colspan="5">
                <div style="overflow:auto;">
                    <div  id="file_div" style="margin-top: 20px;" >
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="5">
                <button onclick="save()" class="btn btn-success">提交</button>
            </td>


        </tr>

    </table>
</div>

</body>
<script type="application/javascript">
    $(function () {

        initData();
        $("[data-type='idCard']").blur(function () {
            /*debugger*/
            var idCard = $(this).val();
            if(!IdentityCodeValid(idCard)){
                /*$(this).focus();*/
                $(this).val("");
                $(this).attr("placeholder","身份证错误");
            }else if(typeof($(this).attr("for-age"))!=="undefined"){
                var ageId = $(this).attr('for-age');
                var age=  GetAge(idCard);
                $("#"+ageId).val(age);
            }
        });
        productRemoveBind();
    });

    function initData(){
        $.ajax({
            url:"/company/companyRegister/find.do",
            type:'post',
            dataType:'json',
            success:function(data){
                console.log(data);
                if(data.success){
                    initPageDate(data.data, '.form-data');
                    addFileInfoToTable(data.data.fileInfoList,'file');
                    var productJson = data.data.productJson;
                    if(!isNullOrUndefined(productJson)){
                        var products = JSON.parse(productJson);
                        var html = "";
                        $(".product-content").remove();
                        $(products).each(function (index,item) {
                            var productName =  item.product;
                            var waste = item.waste;
                            html = html+ "<tr class='product-content'>   "+
                                " <td colspan='2'><input type='text'  class='product' style='width: 245px;' value='"+productName+"' class='form-control form-data '></td>"+
                                " <td colspan='2'><input type='text' class='waste'  style='width: 615px;' value='"+waste+"' class='form-control form-data '></td>"+
                                "<td  class='name' colspan='2'> <button class='product-remove delete'>删除</button> </td>"+
                                "</tr>";
                        });

                        $(html).insertBefore(".product-content-after");
                        productRemoveBind();
                    }

                }
            }
        });
    }
    function save() {

        var params = getPageDataByClass("form-data");
        if (strIsNull(params['name'])) {
            alert("请输入名称");
            return;
        }
        if (!isNumber(params['peopleNum'])) {
            alert("从业人数必须为整数");
            return;
        }
        if (!isNumber(params['truckNum'])) {
            alert("运输车辆必须为整数");
            return;
        }
        params.files = "";
        $("input[name='file_id']").each(function(index,files){
            if(files == "") {
                params.files = $(this).val();
            } else {
                params.files = params.files + "," + $(this).val();
            }
        });


        var img = $("#imgHeadImg").attr("src");
        params['photo']= img;

        var productJson = getProductJson();
        params['productJson']= productJson;
        /*var img = $("#imgDiv img");
         if(img.length > 0) {
         params['image'] = $(img[0]).attr("src");
         }else{
         alert("请上传商品首图");
         return;
         }*/
        $.ajax({
            url: "/company/companyRegister/save.do",
            data: params,
            dataType: 'json',
            type: 'post',
            success: function (data) {
                console.log(data);
                if (data.success) {
                    alert("保存成功");
                    window.location.href = "/html/denglu.html?r="+new Date().getTime();
                } else {
                    alert(data.msg);
                }
            }
        });
    }
    function uploadFile(fileType) {
        $.ajaxFileUpload({
            url: "/file/uploadFile.do?_t=" + new Date().getTime(),            //需要链接到服务器地址
            secureuri: false,//是否启用安全提交，默认为false
            fileElementId: fileType+'_input',                        //文件选择框的id属性
            dataType: 'text',                                     //服务器返回的格式，可以是json, xml
            success: function (data, status) {//服务器成功响应处理函数
                var rpeObj = $.parseJSON(data);
                var fileInfoList = rpeObj.data;
                addFileInfoToTable(fileInfoList,fileType);

            }
        });
    }
    function addFileInfoToTable(fileInfoList,fileType){
        $(fileInfoList).each(function(index,file){
            var fileHtml = "<div  style='border-radius: 2px; width:32%;height:100px;float: left; border: 2px #ffffff solid;'>"+

                "<div align='center' style='height:80px;margin-bottom: 0px;'>"+
                "<div align='center' onclick='downLoadFile(\""+file.path+"\",\""+file.name+"\")' ><input type='hidden' name='"+fileType+"_id' value='"+file.id+"'>"+file.name+"</div>"+

                "<button  style='margin-left: 20px;' " +
                "onclick='downLoadFile(\""+file.path+"\",\""+file.name+"\")' " +
                "name='download' extendData='"+file.id+"'class='btn btn-primary'>下载</button>"+
                "<button  style='margin-left: 20px;' name='delete' extendData='"+file.id+"'class='btn btn-primary delete'>删除</button><div>"
            "</div>";
            $(fileHtml).appendTo($("#"+fileType+"_div"));

        });
        $('.image').viewer();
        $("button[name='delete']").unbind('click');
        $("button[name='delete']").click(function(){
            var id = $(this).attr("extendData");
            $.ajax({
                type: "POST",
                url: "/file/delete.do?_t=" + new Date().getTime(),
                data: "fileId=" + id,

                success: function (result) {
                    alert("删除成功");
                },

                error: function (XMLHttpRequest, textStatus, thrownError) {
                    /* alert("系统异常");*/
                }
            });
            $(this).parent().parent().remove();
        });
    }
    //获取要展示的图片
    function getFileShowUrl(url){
        var extStart=url.lastIndexOf(".");
        var ext=url.substring(extStart,url.length).toUpperCase();
        if(ext!=".BMP"&&ext!=".PNG"&&ext!=".GIF"&&ext!=".JPG"&&ext!=".JPEG"){
            return "/resources/images/timg.jpg";
        }else{
            return url;
        }
    }
    function downLoadFile(url,name) {
        var $a = $("<a></a>").attr("href", url).attr("download", name);
        $a[0].click();
    }
    function productRemoveBind() {
        $(".product-remove").click(function () {
            $(this).parentsUntil("tr").parent().remove();
        });
    }
    function addProduct() {
        var html = "<tr class='product-content'>   "+
            " <td colspan='2'><input type='text'  class='product'  style='width: 245px;' class='form-control form-data '></td>"+
            " <td colspan='2'><input type='text' class='waste'  style='width: 615px;' class='form-control form-data '></td>"+
            "<td colspan='2'> <button class='product-remove delete'>删除</button> </td>"+
            "</tr>";
        $(html).insertBefore(".product-content-after");
        productRemoveBind();
    }
    function getProductJson() {
        var productArray = new Array();
        $(".product-content").each(function (index,item) {

            var product = $(this).find(".product").val();
            var waste = $(this).find(".waste").val();
            productArray.push({"product":product,"waste":waste});
        });
        return JSON.stringify(productArray);
    }
</script>
</html>